package org.ws.edu.task3.core;

import junit.framework.Assert;

import org.junit.BeforeClass;
import org.junit.Test;

public class TestLoaderUnitTest {
    public static TestLoader test;

    @BeforeClass
    public static void init() {
        test = TestLoader.newInstance("example");

    }

    @Test
    public void invalidTestNamesTest() {
        String[] result = test.getInvalidTestNames();
        String[] right = { "InvalidTest.xml" };
        Assert.assertTrue(compare(right, result));

    }

    @Test
    public void availableTestNamesTest() {
        String[] result = test.getAvailableTestNames();
        String[] right = { "CommonExample.xml", "MultiAnswerTest.xml", "ScalesTest.xml", "SingleAnswerTest.xml" };
        Assert.assertTrue(compare(right, result));
    }

    private static boolean compare(String[] array1, String[] array2) {
        if (array1.length != array2.length) {
            return false;
        }
        sort(array1);
        sort(array2);
        for (int i = 0; i < array1.length; i++) {
            if (!array1[i].equals(array2[i])) {
                return false;
            }
        }

        return true;
    }

    private static void change(String[] array, int i, int j) {
        String t = array[i];
        array[i] = array[j];
        array[j] = t;
    }

    private static void sort(String[] array) {
        for (int i = array.length - 1; i >= 0; i--) {
            for (int k = 0; k < i; k++) {
                if (array[k].compareTo(array[k + 1]) > 0) {
                    change(array, k, k + 1);
                }
            }
        }
    }

}
